<mergeNodeAndNetworkRecursively>

When I write "queue" or "HeatQueue", I mean a "complete binary heap".
That data structure is described here: http://en.wikipedia.org/wiki/Binary_heap
The main difference is, for efficiency,
I do not use index 0, and the root is always at index 1.
For example, if array size is a power of 2, the last half of the array is always the leaf level,
the second quarter is the level above that, and the second eigth is the next level.
If the array size is 16, the second 16th is the root, at index 1.

Maybe I will use index 0 for something else.

Trying to answer the question: Can network be an Object array instead of a class?
List of parts and what they must contain:

Network+HeatQueue
	String code
	group of Heat

Heat
	double heat value
	Node
	index in queue
	Network+HeatQueue

Object[] Node
	Heat
	arrays of child nodes and arrays of numbers

Functions:
	heatIncreased
	heatDecreased
	execute Network+HeatQueue
	mainFuncFromNetCode codeString
	newNet codeString
	addNode Net Node






======================IF COMBINE Heat with Node======================

Network
	String code
	group of Node

Object[] Node
	double heat value, index in queue (Network's group of node)
	Network
	arrays of child nodes and arrays of numbers

Functions:
	heatIncreased
	heatDecreased
	execute Network
	mainFuncFromNetCode codeString
	newNet codeString
	addNode Net Node


======================IF INLINE Node's pointer to Network======================

Object[] Node+Network
	double heat value, index in queue (Network's group of node)
	array of all Node in Network (Node executing now would always be at index 1, or maybe 0)
	String code for whole Network
	arrays of child nodes and arrays of numbers

Functions:
	heatIncreased
	heatDecreased
	execute Node+Network
	mainFuncFromNetCode codeString
	newNet codeString
	addNode Node+Network //put Node+Network at index 0 of what was HeatQueue

Problem: How to efficiently add nodes?
	If array of Node is exact size, must copy the array too many times,
		and each time, replace it in all the nodes.
	If array of Node has unused Nodes or nulls at end, that complicates things.

Problem: How to add more Strings of code if we need more complex networks later?

Problem: I forgot about the common arrays in the network.
	Those would have to be stored in each Node also.

======================TRY AGAIN: INLINE Node's pointer to Network======================

Object[] Node+Network
	double heat value
	index in "array of all Node"
	array of common arrays
		array of all Node
		String code
	arrays of child nodes and arrays of numbers


======================TRY ANOTHER VARIATION: INLINE Node's pointer to Network======================

Object[] Node
	index in "array of all Node" and the heat array
	array of common arrays (This is the Network and is never replaced in any Node)
		array of all Node
		array of "double heat value"
		String code
	...arrays of child node and array of numbers...

======================CAN NODE/NETWORK BE RECURSIVE, WHERE NETWORKS CONTAIN NETWORKS...======================

Constraint: The "array of common arrays" must be a node.
Question: Can the "array of common arrays" be a different kind of node than its child?

Object[] Node
	int indexInSupernode
	Object[] supernode
		int indexInSupersupernode
		Object[] supersupernode
		Object[] queue of all Nodes in this "supernode"
		double[] heat values, paired with Object[] above
		String code
	Object[] queue of all Nodes in this "Node"
	double[] heat values, paired with Object[] above
	String thisStringIsNotUsed
	...arrays of child node and array of numbers...


=============================================================================

Object[] Node
	int indexInSupernode
	double heat value
	Object[] supernode
		int indexInSupersupernode
		double heat value
		Object[] supersupernode
		int size used in array below
		Object[] queue of all Nodes in this "supernode"
		String code
		...arrays of childs and arrays of numbers...
	int size used in array below
	Object[] queue of all Nodes in this "Node"
	String wouldBeCodeIfThisNodeWasANetwork
	...arrays of childs and arrays of numbers...


=============================================================================


Object[] Node
	double heat value
	int indexInSupernode
	int size used in array below
	Object[] queue of all Nodes in this "Node"
	String wouldBeCodeIfThisNodeWasANetwork
	Object[] supernode
		double heat value
		int indexInSupersupernode
		int size used in array below
		Object[] queue of all Nodes in this "supernode"
		String code
		Object[] supersupernode
		...arrays of childs and arrays of numbers...
	...arrays of childs and arrays of numbers...

=============================================================================

Should a node be able to be in multiple queues at once, if it has a heat for each?

Object[] Node
	double[] one heat value (of this Node) for each queue in supernode
	int[] one index (of this Node) for each queue in supernode
	//int size used in array below
	//Object[] queue of all Nodes in this "Node"
	//String wouldBeCodeIfThisNodeWasANetwork
	Object[] supernode
		double[] one heat value (of this supernode) for each queue in supersupernode
		int[] one index (of this supernode) for each queue in supersupernode
		int size used in array below
		Object[] queue1 of some set1 Nodes in this "supernode"
		int size2 used in array below
		Object[] queue2 of some set2 Nodes in this "supernode"
		int size3 used in array below
		Object[] queue3 of some set3 Nodes in this "supernode"
		String code
		Object[] supersupernode
		...arrays of childs and arrays of numbers...
	...arrays of childs and arrays of numbers...


=============================================================================


Should a node be able to be in multiple networks, and multiple queues in each network?

Object[] Node
	double[] one heat value (of this Node) for each queue in supernode
	int[] one index (of this Node) for each queue in supernode
	//int size used in array below
	//Object[] queue of all Nodes in this "Node"
	//String wouldBeCodeIfThisNodeWasANetwork
	Object[] eachSupernode
		Object[] supernode
			double[] one heat value (of this supernode) for each queue in supersupernode
			int[] one index (of this supernode) for each queue in supersupernode
			int size used in array below
			Object[] queue1 of some set1 Nodes in this "supernode"
			int size2 used in array below
			Object[] queue2 of some set2 Nodes in this "supernode"
			int size3 used in array below
			Object[] queue3 of some set3 Nodes in this "supernode"
			String code
			Object[] supersupernode
			...arrays of childs and arrays of numbers...
	...arrays of childs and arrays of numbers...

The double[] heat values and int[] indexs would have to be multiple arrays if there are multiple supernodes per node.

All this would take a lot of memory.


=============================================================================


Object[] Node
	Object[] eachLinkToSupernode
		Object[] linkToSupernode1
			double[] one heat value (of this Node) for each queue in supernode
			int[] one index (of this Node) for each queue in supernode
			Object[] supernode
				Object[] eachLinkToSupersupernode
					double[] one heat value (of this supernode) for each queue in supersupernode
					int[] one index (of this supernode) for each queue in supersupernode
					Object[] supersupernode
				Object[] eachLinkToQueue
					Object[] queue1
						int size used in array below
						Object[] queue1 of some set1 Nodes in this "supernode"
					Object[] queue1
						int size2 used in array below
						Object[] queue2 of some set2 Nodes in this "supernode"
					Object[] queue1
						int size3 used in array below
						Object[] queue3 of some set3 Nodes in this "supernode"
				String code
				...arrays of childs and arrays of numbers...
		Object[] linkToSupernode2
			double[] heats
			int[] indexs
			Object[] supernode
	...arrays of childs and arrays of numbers...


This is getting too complicated to be useful. The 2 important questions are:
* Multiple nets per node?
* Multiple queues per net?

Are queues like pointers to child nodes, but with required interaction with parent?

If a network has 10000 nodes and each node has 100 childs,
could that be represented as 10000 networks which each
point forward (contents of 1 queue) to 100 networks
and backward (heat and index for each queue in parent) to 100 networks?

When a node uses its 1 queue, and node x is first in that queue, how does it know which of x's 100 heats and 100 indexs is for this node's 1 queue?

Object[] Node
	...
	Object[] childQueue
	double[] childHeat
	int[] indexInQueueOfEachParent //same order as array below
	Object[] parents //in what order
	...


===================================================

If a parent node executes its child nodes, and childs never change anything in the parent, then "int[] indexInQueueOfEachParent" and "Object[] parents" are not needed.

[Comment added 2/9/09]
	Child nodes need to modify things in their parent, but this will be done by defining constant-depth recursion (and separately a type of variable-depth recursion on top of that) that can see the parent and child simultaneously. Example: In a heapQueue having flo values for all nodes in a network (and a network is the same as a node but has more childs), the childs of the childs could be looked up in the first node's heapQueue and change their flo value which causes them to move in the heapQueue. A simple neural network would do that.
	Also, nodes do not contain information about their parent's heapQueue, because that allows them to be in many heapQueues at once, which allows a network and node to be the same type of thing. Most nodes will have an empty heapQueue, and nodes used as networks will usually have at least 1 large heapQueue containing other nodes of their same type with usually empty heapQueues. An empty heapQueue is 3 empty arrays: int[0], int[0], and flo[0].
[END Comment added 2/9/09]

Object[] Node
	...
	Object[] childQueue
	double[] childHeat
	...

Example hybrid of network + neural node + bayesian node:

Object[] Node
	...
	Object[] childQueue
	double[] childHeat
	Object[] bayesianChilds //size between 1 and 7
	double[] bayesianWeights //size 2^bayesianChilds
	...

Try this simpler example, temporarily, to figure out how to recursively do control-flow.

Heat should flow recursively through nodes and decrease in a parent node as it flows, one at a time,
to the child nodes that parent node thinks are hottest.

Object[] Node
	double neuralActivation
	Object[] childQueue
	double[] childHeat
	double[] childWeight
	...

(About efficiency of something else: That is 3 arrays in the same queue. Each swap must be done 3 times. Instead, an int array could contain index into those arrays, to swap in only 1 array. That complexity should be avoided if its practical.)

doubles in childHeat change often.
doubles in childWeight change rarely.

While the network executes, whatever sequence of nodes execute (before popping themselves off the stack of nodes), should a node be created with those as childs, and weights higher for nodes earlier in the sequence? There are many ways to do control-flow.

int nodeStackSize
Object[] nodeStack

The simplest design so far is to have 1 queue per network and nodes are not the same type of thing as a network. In this system where nodes and networks are the same type of thing, how would that be defined?

Object[] BayesianNetwork
	Object[] nodeQueue
	double[] nodeHeat
	String netCode
	...

Object[] BayesianNode
	double[] chanceCertainCurios //size 3
	Object[] bayesianNodes //size 0-7
	double[] bayesianWeights //size 2^bayesianNodes
	...

nodeStack = [BayesianNetwork, BayesianNetwork.nodeQueue[1]] //BayesianNetwork.nodeQueue[1] changes in most iterations

Loop until BayesianNetwork.nodeHeat[1] <= 0, or if that does not happen, until some arbitrary number of iterations.

If loop at most 500 times, could set heat to 500 in a parent network:

Object[] ParentNetwork
	Object[] nodeQueue
		(ignoreIndex0)
		Object[] BayesianNetwork
			Object[] nodeQueue
			double[] nodeHeat
			String netCode
			...
		...more networks...
	double[] nodeHeat
		(ignoreIndex0)
		500
		...heats of more networks...
	String parentNetCode
	...

nodeStack = [ParentNetwork, BayesianNetwork, BayesianNode]

For each node, for each bayesian weight, execute a floatfunc on heat, ..., weight.

Should creating heat be prevented, but allow moving heat?
1 heat could be consumed for each array index read and write, and some other measure for executing floatfuncs.

Extra heat could be stored in 1 double per node, and moved to heat vars when its needed,
but that would add the cost of checking if its negative every time that happens.
Or some vars could be defined that move heat. For example, a floatfunc would execute on
these things in a node: x, y, z, childWeight. If x is positive,
move that much heat from the extra heat var to the parent node.
If y or z is positive, move heat somewhere else too. How much to move is the the value of x y or z.


============================
These are some data structures that maybe should be always available in Audivolv code:
	heatQueue
	arrayLinear
	array*
	array^
============================

Example of control-flow separated from heat:

Object[] Node
	Object[] childQueue
	double[] childHeatForSorting //this can be created and is used for sorting
	double[] childHeatNoncreatable //this is for allocating cpu time
	...

That could be optimized at runtime like this:

public class QueueItem53{
	public Object child;
	public double heatForSorting, heatNoncreatable;
}

Object[] Node
	QueueItem53[] queue //can swap indexs faster in this 1 array than 3 arrays
	...

Optimizations are less important than choosing the design of Audivolv. Focus on design for now, but stay compatible with possibly adding those optimizations later.


Control-flow can be defined in many ways, and which ways should be evolvable.

Example ways to do control-flow:
* 0-level, within the current node/network only.
* 1 level, nodes within network/parentNode execute and update their heat in the network/parentNode. Always execute the node with the highest heat in that 1 network/parentNode.
* N-level bounded recursion. Same as "1 level" above, but is maximum of "N level". Would be less than N if all heats in a node are nonpositive.
* Unbounded recursion, but bounded total cycles by transferring heat but never creating heat.

When recursing through nodes, and the current node has many childs in each of 2 queues, which queue does control-flow use?
Does it use both? Does it use multiple nodes per queue?
Does it do a breadth-first traversal instead of depth-first?

Should the way of doing control-flow be evolvable?

The simplest design so far is for a "nodeFunc" to not recurse, and iterate some number of times based on some combination (allowing duplicates) of its array sizes (and Integers viewed as unusable arrays).
Similar to an iteration the size of one of those arrays or Integers, an iteration could be done on a queue. A queue is an array of node and a mirrored double array the same size, where higher double values move nodes toward the top of the queue.

Simpler than 0 or 1 or N or unbounded recursion, control-flow can be divided into recursive and nonrecursive.
If arrays in child nodes are used but no function is called on the child node, thats nonrecursive.
If its possible a function could be called on any child, its recursive.

It may not be necessary to divide the types of control-flow.
Loops of both types could be defined in the same string of code.
But total running time must be bounded to avoid infinite loops in evolved code.

do#nodeFunc(
	binheap#childNodesB
	arrayDef#childNodesC(Object 30)
	binheapRootLoop#loopB1(
		childNodesB
		//loop how many times?
		arrayLoop(
			childNodesC
			+=float(
				childNodesC.binheapSortValue
				//If its a binheapRootLoop, always use index 1 in the binheap.
				childNodesB.someFloatVarName //means childNodesB.1.someFloatVarName
			)
		)
	)
)

Should a binheap be allowed to be written as 1 thing like that? Its 2 arrays: Object[] and double[].

do#nodeFunc(
	arrayDef#binheapObjectArrayB(Object ...)
	arrayDef#binheapFloatArrayB(double binheapObjectArrayB)
	arrayDef#childNodesC(Object 30)
	binheapRootLoop#loopB1(
		binheapObjectArrayB
		binheapFloatArrayB
		//loop how many times?
		arrayLoop(
			childNodesC
			+=float(
				childNodesC.binheapSortValue
				//If its a binheapRootLoop, always use index 1 in the binheap.
				binheapFloatArrayB //means binheapFloatArrayB.1.someFloatVarName
			)
		)
	)
)

Should multiple node types be allowed in the same network if they are defined recursively that way, like twoArraysOfBinheap is a type of node below? twoArraysOfBinheap is very much like a node because the indexs in its 2 nodes are paired, and the sizes of the arrays must be equal.

Can the dimensions of a 2d array be swapped, like twoArraysOfBinheap(2 arrays) be swapped for binheapOfSize2Arrays(list(a b) list(c d)...)?

Should each node have a CURENTCHILD, like a binheap has a first node?

do#nodeFunc(
	twoArraysOfBinheap#taobh(
		arrayDef#binheapObjectArrayB(Object ...)
		arrayDef#binheapFloatArrayB(double binheapObjectArrayB)
	)
	arrayDef#childNodesC(Object 30)
	binheapRootLoop#loopB1(
		taobh
		//loop how many times?
		arrayLoop(
			childNodesC
			+=float(
				childNodesC.binheapSortValue
				//If its a binheapRootLoop, always use index 1 in the binheap.
				binheapFloatArrayB //means binheapFloatArrayB.1.someFloatVarName
			)
		)
	)
)


An Object array of constant size could be viewed as a node. It would be simpler if traversal toward "depth-first" alternated constant size and variable size arrays, but it is inefficient because constant-size double arrays could not be used.

A very important thing that Audivolv's networks must always do easily is iterating simultaneously through bayes child list and bayes weight list which is exponentially bigger.

do#node(
	list(arrayDef(double 3))
	arrayDef#bayesChilds(Object 3)
	arrayDef#bayesWeights(double ^(2 bayesChilds))
	loop(

	)
)


</mergeNodeAndNetworkRecursively>